import prettier from 'prettier/standalone'
import {onMounted, ref} from 'vue'

export function usePrettierHandle(){
    const monacoEditor=ref(null)

    async function prettierFormat(options={}){
    //    格式化
        const {parser,plugins,...extraOp}=options
        try {
            let defaultOption = {
                semi: true,
                trailingComma: 'all',
                singleQuote: true,
                printWidth: 80,
                tabWidth: 4,
                bracketSameLine:true,
                htmlWhitespaceSensitivity:'ignore'
            }
            Object.assign(defaultOption,extraOp)
            const sourceCode= monacoEditor.value.getValue()
            const formatCode = await prettier.format(sourceCode, {
                parser,
                plugins,// 根据需要添加其他解析器插件
                ...defaultOption
            })
            monacoEditor.value.setValue(formatCode)

        } catch (error) {
            console.error('Error formatting code:', error)
        }
    }
    return {
        monacoEditor,
        prettierFormat
    }
}
